package cn.buddha.jesj.jvm.classloader;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @ClassName ClassLoaderTest
 * @Author 友野浩二
 * @Date 2020/5/23 12:57
 * @Description 双亲委派机制测试
 * @Version 1.0
 */
public class ClassLoaderTest {

    public static void main(String[] args) {
        // 查看当前系统类路径中包含的路径条目
        String property = System.getProperty("java.class.path");
        String[] propValues = property.split(";", -1);
        for (String propValue : propValues) {
            System.out.println(propValue);
        }

        System.out.println("==================================================");
        String testBeanName = "cn.buddha.jesj.jvm.classloader.TestBean";
        try {
            // 调用加载当前类的类加载器（这里即为系统类加载器）加载TestBean
            Class<?> aClass = Class.forName(testBeanName);
            // 查看被加载的TestBean类型是被那个类加载器加载的
            System.out.println(aClass.getClassLoader());// sun.misc.Launcher$AppClassLoader@18b4aac2
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            Constructor<TestBean> constructor = TestBean.class.getConstructor(int.class);
            TestBean testBean = constructor.newInstance(123);
            System.out.println(testBean.getA());
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}
